import request from '@/router/axios';
import {baseUrl} from '@/config/env';
import {getToken} from "@/utils/auth";

const getCurrentContacts = (data) => {
    return request({
            url: baseUrl + '/chat/contacts/getCurrentContacts',
            method: 'get',
            data: data
        }
    )
}
const getChatHistory = (data) => {
    return request({
            url: baseUrl + '/chat/history/getChatHistory',
            method: 'get',
            data: data
        }
    )
}

const initWebSocket = (hostSocketUrl) => {// 进入页面创建websocket连接
    if (!window.WebSocket) {// 判断页面有没有存在websocket连接
        return
    }
    if(hostSocketUrl)  return  new WebSocket(hostSocketUrl);
    let protocol="ws://"
    const serverHot = window.location.hostname;
    let port=window.location.port;
    if (window.location.protocol.indexOf("https") ) {
        protocol="wss://"
    }
    let url =protocol+serverHot+":"+port+baseUrl+"/chat/"+getToken();
    return new WebSocket(url);
}

const chatApi = {
    initWebSocket,
    getCurrentContacts,
    getChatHistory
}
export default chatApi
